package
{
	import org.flixel.*;

	public class MiniPuzzleListing extends FlxGroup
	{
		public static const LISTING_WIDTH:int = MiniPuzzleLoader.CONTAINER_WIDTH - 80;
		public static const LISTING_HEIGHT:int = 60;
		
		public static const NORMAL_MODE:int = 0;
		public static const CHALLENGE_MODE:int = 1;

		// Format will be:
		// 0-map id
		// 1-playerid
		// 2-name
		// 3-contents
		// 4-arrows
		// 5-chickens
		// 6-difficulty
		// 7-tileset
		// 8-rating
		// 9-number of votes
		// 10-#ofTimesBeaten
		// 11-#ofTimesPlayed
		
		private var bg:FlxSprite;
		private var titleText:FlxText;
		private var subtitleText:FlxText;
		private var overlap:Boolean;
		private var specArray:Array;
		
		private var friendName:String;
		private var friendID:String;
		private var mode:int;
		
		public function MiniPuzzleListing(_title:String, _author:String, _rating:int, spec:String, x:int, y:int, friendName:String, friendID:String, mode:int=NORMAL_MODE)
		{
			this.mode = mode;
			this.friendName = friendName;
			this.friendID = friendID;
			
			bg = new FlxSprite();
			bg.makeGraphic(LISTING_WIDTH, LISTING_HEIGHT, 0x000000, true);
			bg.drawRoundRect(0, 0, LISTING_WIDTH, LISTING_HEIGHT, 20, 0x000000);
			bg.x = x;
			bg.y = y;
			add(bg);
			
			specArray = spec.split(":");
			
			if (mode == NORMAL_MODE) {
				drawNormalListing(_title, _author, _rating);
			} else if (mode == CHALLENGE_MODE) {
				drawChallengeListing(_title, _author);
			}
		}
		
		public function drawNormalListing(_title:String, _author:String, _rating:int):void {
			titleText = new FlxText(bg.x + 5, bg.y + 5, LISTING_WIDTH, _title, false);
			titleText.setFormat("Courier New", 16, 0xFFFF00);
			add(titleText);
			
			var text:String = specArray[11] + " plays, " + specArray[10] + " of wins";
			subtitleText = new FlxText(titleText.x, titleText.y + titleText.height, LISTING_WIDTH * .8, text, false);
			subtitleText.setFormat("Courier New", 14, 0xFFFFFF);
			add(subtitleText);
			
			drawStars(_rating);
		}
		
		public function drawChallengeListing(_title:String, _author:String):void {
			titleText = new FlxText(bg.x + 5, bg.y + 5, LISTING_WIDTH, _title, false);
			titleText.setFormat("Courier New", 16, 0xFFFF00);
			add(titleText);
			
			var author:String = Elemental.fbInfo.getFullNameFromID(_author);
			if (author == "") {
				author = _author;
			}
			var text:String = "created by " + author;
			subtitleText = new FlxText(titleText.x, titleText.y + titleText.height, LISTING_WIDTH, text, false);
			subtitleText.setFormat("Courier New", 14, 0xFFFFFF);
			add(subtitleText);
		}
		
		public override function update():void {
			super.update();
			if (bg.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				if (!overlap) {
					overlap = true;
					bg.drawRoundRect(0, 0, LISTING_WIDTH, LISTING_HEIGHT, 20, 0x222222);
				}
			} else if (overlap) {
				overlap = false;
				bg.drawRoundRect(0, 0, LISTING_WIDTH, LISTING_HEIGHT, 20, 0x000000);
			}
			
			if (FlxG.mouse.justPressed() && overlap) {
				startPuzzle();
			}
		}
		
		public function startPuzzle():void {
			var arrows:Array = String(specArray[4]).split(",");
			for (var i:int = 0; i < 4; i++) {
				arrows[i] = int(arrows[i]);
			}
			
			var chickens:Array = String(specArray[5]).split(",");
			Elemental.Log.logPlayerMapPlayed(specArray[0]);
			var ps:PuzzleSpecification = new PuzzleSpecification(specArray[3], specArray[7], arrows, chickens, specArray[0]);
			if (mode == MiniPuzzleListing.CHALLENGE_MODE) {
				FlxG.switchState(new PuzzlePlayingState(ps, true, false, false, false, false,friendName,friendID ));
			} else {
				FlxG.switchState(new PuzzlePlayingState(ps, false, true, false, false, false,friendName,friendID ));
			}
		}
		
		/**
		 * Draws the 5 stars
		 */
		private function drawStars(rating:int):void {
			var xPos:int = (bg.x + bg.width) - (20*5) - 10;
			for (var i:int = 0; i < 5; i++) {
				var star:Class = (i + 1 <= rating) ? Res.MiniStarFull : Res.MiniStarEmpty;
				add(new FlxSprite(xPos, bg.y + 10, star));
				xPos += 20;
			}
		}
	}
}